package com.asynctm.android.common.application;

import roboguice.activity.RoboActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.asynctm.android.R;
import com.asynctm.android.about.activity.AboutActivity;
import com.asynctm.android.common.activity.AppMenuActivity;
import com.asynctm.android.common.activity.MenuButtonElementViewHolder;
import com.asynctm.android.socialnetwork.activity.SocialNetworkActivity;
import com.asynctm.android.tm.activity.TaskManagerActivity;

/**
 * Actividad base para todas las actividades de la aplicacion
 * 
 * @author rockdrigo
 */
public class BaseActivity extends RoboActivity {

	private String activityName;

	/**
	 * Pide las variables que nesesarias para el despliege del menu
	 * 
	 * @param actividad
	 * @param nombre
	 */
	protected void setName(String nombre) {
		activityName = nombre;
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onRestart() {
		super.onRestart();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void onPause() {
		super.onPause();

	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		MenuButtonElementViewHolder menuButtonElementViewHolder = new MenuButtonElementViewHolder(
				getApplicationContext());

		menuButtonElementViewHolder.populateMenu(menu);

		return super.onCreateOptionsMenu(menu);

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		Intent intent = null;

		switch (item.getTitle().charAt(0)) {

		case 'H':
			if (!(this.activityName.equals(AppMenuActivity.class
					.getSimpleName().toString()))) {
				finish();
			}
			break;

		case 'T':
			if (!(this.activityName.equals(TaskManagerActivity.class
					.getSimpleName().toString()))) {
				intent = new Intent(getApplicationContext(),
						TaskManagerActivity.class);
			}
			break;

		case 'R':
			if (!(this.activityName.equals(SocialNetworkActivity.class
					.getSimpleName().toString()))) {
				intent = new Intent(getApplicationContext(),
						SocialNetworkActivity.class);
			}
			break;

		case 'A':
			if (!(this.activityName.equals(AboutActivity.class.getSimpleName()
					.toString()))) {
				intent = new Intent(getApplicationContext(),
						AboutActivity.class);
			}
			break;
		}

		if (intent != null) {
			startActivity(intent);
			if (!(this.activityName.equals(AppMenuActivity.class
					.getSimpleName().toString()))) {
				finish();
			}
		} else if (!(this.isFinishing())) {
			Toast.makeText(
					getApplicationContext(),
					getText(R.string.msg_activity_stay).toString() + " "
							+ item.getTitle().toString(), Toast.LENGTH_SHORT)
					.show();
		}

		return super.onOptionsItemSelected(item);
	}

}
